package com.hnevc.netfoodchoose;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.SeekBar;

import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class SettingActivity extends AppCompatActivity {
    EditText mEtUserName;
    RadioGroup mRgGender;
    CheckBox mCbFish,mCbHot,mCbSour;
    SeekBar mSbBudget;//预算
    Button mBtnFind;//查找按钮
    String gender;
    int budget;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        initEvent();
    }

    private void initView() {
        mEtUserName = (EditText) findViewById(R.id.id_et_username);
        mRgGender = (RadioGroup) findViewById(R.id.id_rg_sex);
        mSbBudget = (SeekBar) findViewById(R.id.id_sk_budget);
        mCbFish = (CheckBox) findViewById(R.id.id_cb_fish);
        mCbHot = (CheckBox) findViewById(R.id.id_cb_hot);
        mCbSour = (CheckBox) findViewById(R.id.id_cb_sour);
        mBtnFind = (Button) findViewById(R.id.id_btn_findfood);
    }
    private void initEvent() {
        mRgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if(checkedId==R.id.id_rb_male)
                    gender = "男";
                else
                    gender ="女";
            }
        });
        mBtnFind.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
            //TODO 获取设置数据，并将数据传递给ShowFoodActivity
            public void onClick(View view){
                    Food food=new Food();
                    food.setFoodSour(mCbSour.isChecked()?1:0);
                    food.setFoodHot(mCbHot.isChecked()?1:o);
                    food.setFoodFish(mCbFish.isChecked()?1:0);
                    food.setFoodPrice(budget);

                    new Thread().start();
                }

            new Thread(new Runnable() {
                @Override
                public void run() {
                    JSONObject object=new JSONObject();
                    try{
                        object.put("foodHot",food.getFoodHot());
                        object.put("foodSour",food.getFoodSour());
                        object.put("foodFish",food.getFoodFish());
                        object.put("foodPrice",food.getFoodPrice());
                    }
                    catch(JSONException e){
                        e.printStackTrace();
                    }
                    String data=object.toString();
                }
            }).start();
                OkHttpClient client=new OkHttpClient.Builder()
                        .cookieJar(new MyCookieJar())
                        .build();
                RequestBody body=RequestBody.create(MediaType.parse("application/json"),data);

                String urls="http://"
                        +getResources().getString(R.string.ServerIp)
                        +":8080/RestaurantServer/Food/doFindFoodJson";
                Request request=new Request.Builder()
                        .url(urls)
                        .post(body)
                        .build();
                try{
                    Response response=client.newCall(request).execute();
                    String s=response.body().string();
                    Gson gson=new Gson();
                    List<Food> foodList=
                             gson.fromJson(s,new TypeToken<List<Food>>(){}.getType());
                    Log.e
                }


            }
        });
        mSbBudget.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser){
                    budget =(int) progress;
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }
}

